package com.weitu.majorHazard.activityRecord.controller;

import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.majorHazard.activityRecord.entity.ActivityRecord;
import com.weitu.majorHazard.activityRecord.entity.ProblemAssociation;
import com.weitu.majorHazard.activityRecord.service.ActivityRecordService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/4/24.
 */
@Controller
@RequestMapping(value = "majorHazard/activityRecord")
public class ActivityRecordController extends BaseController {
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"majorHazard/activityRecord";
    private final ActivityRecordService activityRecordService;
    private final AccessoryUtilService accessoryUtilService;
    @Autowired
    public ActivityRecordController(ActivityRecordService activityRecordService,AccessoryUtilService accessoryUtilService){
        this.activityRecordService=activityRecordService;
        this.accessoryUtilService=accessoryUtilService;
    }

    /**
     * 调到活动记录列表页
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 活动记录列表数据
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String,Object> paramMap=new HashMap<String,Object>();
        putParametersIntoMap(paramMap);
        int count=this.activityRecordService.queryCount(paramMap);
        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);
        PageView pageView=dataGridPagination.getPageView();

        paramMap.put("pageView",pageView);

        List<ActivityRecord> activityRecordList=this.activityRecordService.queryList(paramMap);

        return dataGridPagination.flushDataGridData(activityRecordList);

    }

    /**
     * 跳到添加页面
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("addUserId",BaseController.getSessionUser().getId());
        map.put("addUserName",BaseController.getSessionUser().getRealName());
        map.put("addDeptId",BaseController.getSessionUser().getDepartment().getId());
        map.put("addDeptName",BaseController.getSessionUser().getDepartment().getName());
        map.put("addTime", DateTimeUtil.formatterDateTime());
        modelAndView.addObject("userMessage",map);
        modelAndView.setViewName(MODULE_BATH_PATH+"/add");
        return modelAndView;
    }

    /**
     * 添加
     * @param activityRecord
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @SystemLog(module = "活动记录",methods = "添加活动记录")
    public AjaxResult addAjax(ActivityRecord activityRecord){
        activityRecord.setId(IDGenerator.getId());
        activityRecord.setDelState(0);
        boolean flag=this.activityRecordService.insertActivityRecord(activityRecord);
        if(flag){
            return AjaxResult.success("添加成功");
        }else {
            return AjaxResult.fail("添加失败");
        }
    }

    /**
     * 跳入编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public ModelAndView edit(String id){
        ActivityRecord activityRecord=this.activityRecordService.queryById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("activityRecord",activityRecord);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/edit");
        return modelAndView;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ModelAndView detail(String id){
        ActivityRecord activityRecord=this.activityRecordService.queryById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("activityRecord",activityRecord);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("uploadUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/detail");
        return modelAndView;
    }
    /**
     * 修改活动记录
     * @param activityRecord
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @SystemLog(module = "活动记录",methods = "修改活动记录")
    public AjaxResult editAjax(ActivityRecord activityRecord){
        activityRecord.setUpdateUserId(BaseController.getSessionUser().getId());
        activityRecord.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        activityRecord.setUpdateTime(DateTimeUtil.formatterDateTime());
        boolean flag=this.activityRecordService.updateActivityRecord(activityRecord);
        if(flag){
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.fail("修改失败");
        }
    }
    /**
     * 问题页面
     * @param keyDevice
     * @param inspectedUnit
     * @return
     */
    @RequestMapping(value = "/problem",method = RequestMethod.GET)
    public ModelAndView problem(String keyDevice,String inspectedUnit,String activityRecordId){
        ModelAndView modelAndView=new ModelAndView();
        Map<String,Object> paramMap=new HashMap<String,Object>();
        paramMap.put("keyDevice",keyDevice);
        paramMap.put("inspectedUnit",inspectedUnit);
        paramMap.put("activityRecordId",activityRecordId);
        paramMap.put("userId",BaseController.getSessionUser().getId());
        paramMap.put("userRealName",BaseController.getSessionUser().getRealName());
        modelAndView.addObject("message",paramMap);
        modelAndView.setViewName(MODULE_BATH_PATH+"/problem");
        return modelAndView;
    }
    /**
     * 保存问题
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/problem",method = RequestMethod.POST)
    @SystemLog(module = "活动记录",methods = "保存问题")
    public Map<String,Object> problemAjax(ProblemAssociation problemAssociation){
        problemAssociation.setId(IDGenerator.getId());
        problemAssociation.setDelState(0);
        problemAssociation.setAddUserId(BaseController.getSessionUser().getId());
        problemAssociation.setAddDeptId(BaseController.getSessionUser().getDepartment().getId());
        problemAssociation.setAddTime(DateTimeUtil.formatterDateTime());
        boolean flag=this.activityRecordService.insertProblemAssociation(problemAssociation);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",problemAssociation.getId());
        if(flag){
            map.put("result",AjaxResult.success("添加成功"));
            return map;
        }else {
            map.put("result",AjaxResult.fail("添加失败"));
            return map;
        }
    }

    /**
     * 问题列表
     * @param activityRecordId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/problemList",method = RequestMethod.POST)
    public List<ProblemAssociation> problemList(String activityRecordId){
        List<ProblemAssociation> problemAssociationList=this.activityRecordService.queryProblemAssociation(activityRecordId);
        return problemAssociationList;
    }

    /**
     * 删除problem
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/problemDelete",method = RequestMethod.POST)
    @SystemLog(module = "活动记录",methods = "删除问题")
    public AjaxResult problemDelete(String id){
        int count=this.activityRecordService.deleteProblemAssociation(id);
        if(count==1){
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.fail("删除失败");
        }
    }

    /**
     * 详情列表
     * @param id
     * @return
     */
    @RequestMapping(value = "/problemEdit",method = RequestMethod.GET)
    public ModelAndView problemEdit(String id){
        ProblemAssociation problemAssociation=this.activityRecordService.queryProblemAssociationById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("problemAssociation",problemAssociation);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("addUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/problemEdit");
        return modelAndView;
    }

    /**
     * 问题详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/problemDetail",method = RequestMethod.GET)
    public ModelAndView problemDetail(String id){
        ProblemAssociation problemAssociation=this.activityRecordService.queryProblemAssociationById(id);
        List<AccessoryPackageInfo> accessoryList =  accessoryUtilService.queryAccessoryByBusinessId(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("problemAssociation",problemAssociation);
        modelAndView.addObject("accessoryList",accessoryList);
        modelAndView.addObject("addUserId",BaseController.getSessionUser().getId());
        modelAndView.setViewName(MODULE_BATH_PATH+"/problemDetail");
        return modelAndView;
    }
    /**
     * 修改问题
     * @param problemAssociation
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/problemEdit",method = RequestMethod.POST)
    @SystemLog(module = "活动记录",methods = "修改问题")
    public Map<String,Object> problemEditAjax(ProblemAssociation problemAssociation){
        problemAssociation.setUpdateUserId(BaseController.getSessionUser().getId());
        problemAssociation.setUpdateDeptId(BaseController.getSessionUser().getDepartment().getId());
        problemAssociation.setUpdateTime(DateTimeUtil.formatterDateTime());
        boolean flag=this.activityRecordService.updateProblemAssociation(problemAssociation);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("id",problemAssociation.getId());
        if(flag){
            map.put("result",AjaxResult.success("修改成功"));
            return map;
        }else {
            map.put("result",AjaxResult.fail("修改失败"));
            return map;
        }
    }
}
